﻿using MMK.Paket_Tracker.Runtime;
using System.Windows.Controls;
using System.Windows.Input;
using MMK.Paket_Tracker.Runtime.Commands;
using MMK.Paket_Tracker.Runtime.BusinessEntities;
using MMK.Paket_Tracker.GUI.Models;

namespace MMK.Paket_Tracker.GUI.ViewModels
{
    /// <summary>
    /// Get Weight View Model
    /// </summary>
    public class GetWeightViewModel : ViewModelBase
    {
        /// <summary>
        /// Gets the status view model.
        /// </summary>
        /// <param name="owner">The owner.</param>
        public GetWeightViewModel(UserControl owner)
        {
            this.owner = owner;
            this.model = new GetWeightModel();
            PrepareCommands();
        }

        /// <summary>
        /// Gets or sets the data for the datacontext.
        /// </summary>
        /// <value>The data.</value>
        public GetWeightModel Data
        {
            get { return model; }
            set { model = value; }
        }

        private void PrepareCommands()
        {
            CommandBinding goOn = new CommandBinding(PTCommands.GoOn);
            CommandBinding goBack = new CommandBinding(PTCommands.GoBack);

            goOn.Executed += goOn_Executed;
            goOn.CanExecute += goOn_CanExecute;

            goBack.Executed += goBack_Executed;
            goBack.CanExecute += goBack_CanExecute;

            owner.CommandBindings.Add(goBack);
            owner.CommandBindings.Add(goOn);
        }

        void goBack_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        void goBack_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            BusinessLogic.BusinessLogic.GetWeightModel = null;
            NotifyMainWindow(new MainWindowNotification(((UserControl)sender).Name, null, (RoutedUICommand)e.Command));
        }

        void goOn_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (model != null)
            {
                e.CanExecute = !(model.HasError);
            }
        }

        void goOn_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            BusinessLogic.BusinessLogic.GetWeightModel = Data;
            NotifyMainWindow(new MainWindowNotification(((UserControl)sender).Name, null, (RoutedUICommand)e.Command));
        }

        private GetWeightModel model;
        
    }
}
